package com.kd.xjoa.scheduled.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kd.xjoa.scheduled.service.CombService;
import com.kd.xjoa.scheduled.utils.ScheduledCommonUtil;

@RestController
@RequestMapping("/api")
public class CombController {
	
	private static final Log logger = LogFactory.getLog(CombController.class);

	@Autowired
	private CombService combService;
	
	@RequestMapping("/comb")
	public Map<String,Object> totalComb(String beginDate,String endDate){
		Map<String,Object> map = new HashMap<String,Object>();
		if(StringUtils.isBlank(beginDate) || StringUtils.isBlank(endDate)){
			map.put("code","-2");
			map.put("message","参数不对");
			return map;
		}
		if(beginDate.length() != 8 || endDate.length() != 8){
			map.put("code","-2");
			map.put("message","参数不对");
			return map;
		}
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		try {
			List<Date> list = ScheduledCommonUtil.getBetweenDates(sdf.parse(beginDate), sdf.parse(endDate));
			if(list == null || list.size() < 1){
				map.put("code","-3");
				map.put("message","起始时间不能大于接受时间");
				return map;
			}
			if(list.size() > 10){
				map.put("code","-3");
				map.put("message","单次统计时间范围不能超过10天");
				return map;
			}
			
			for (Date date : list) {
				Integer result = combService.totalCombMonitor(sdf.format(date));
				logger.info("统计日期："+sdf.format(date)+"，执行插入条数："+result);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		map.put("code","200");
		map.put("message","success");
		return map;
	}
}
